class window.Item extends window.Entity
  constructor : () ->
    @z = 0
    @dx = 0
    @dy = 0
    @weight = 0.01
    @zspeed = 0
    @carriedBy = null
    @spr = [0, 6 * 16]
    @soundMultipler = 1.0
    @airFriction = 0.98
    @renderTransform = new window.Transform2D()

  init : (@g) ->
    super @g
    @addTag "item"

  pickup : (who) ->
    who.isCarrying = @
    @carriedBy = who
    @z = who.height

  hurt : () ->

  hurl : (who, @dx, @dy) ->
    @dx ?= 0
    @dy ?= 0

    who.isCarrying = null
    @carriedBy = null

    @z = who.height
    @zspeed = -1.0
  
  handleCarried : ->
    return false if @carriedBy == null

    @x = @carriedBy.x
    @y = @carriedBy.y

  handleNotCarried : ->
    return false if @carriedBy != null
    return false if @z <= 0

    @z -= @zspeed
    @zspeed += 0.1 
    @z = 0 if @z < 0.0

    @x += @dx
    @dx *= @airFriction
    @y += @dy
    @dy *= @airFriction

  slideCollide : (c) ->
    return false if @carriedBy != null


    @x = c[0]
    @y = c[1]

    
    l = @dx * @dx + @dy * @dy
    @g.makeSound @x, @y, l * @weight * 2.0 * @soundMultipler

    @dx = @dx * 0.25
    @dy = @dy * 0.25


  collidedWith : (who) ->
    return false if @isCarried()
    return false if not who.hasTag "guard"
    return false if who.isDead()


    l = @dx * @dx + @dy * @dy
    @g.makeSound who.x, who.y, l * @weight * @soundMultipler

    who.edx += @dx
    who.edy += @dy

    @dx = @dx * 0.25
    @dy = @dy * 0.25
    who.hurt l * @weight * 0.6

    if  who.isDead()
        @g.makeBlood who.x, who.y
    else 
        who.agro() 

    true

  isCarried : -> @carriedBy != null
  
  update : () ->
    @handleCarried()
    @handleNotCarried()

  render : () ->
    @transform.setTranslation @x, @y - @z
    @g.draw.drawSprite @spr, @transform